package com.sqfw.project.wpReport.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.common.constant.HttpStatus;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.page.PageDomain;
import com.sqfw.framework.web.page.TableSupport;
import com.sqfw.project.wp.domain.WpInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wpReport.domain.WpInfoDept;
import com.sqfw.project.wpReport.service.IWpInfoDeptService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 三级承办单位处理情况汇总表Controller
 *
 * @author lx
 * @date 2023-07-11
 */
@RestController
@RequestMapping("/wpReport/wpInfoDept")
public class WpInfoDeptController extends BaseController {
    @Autowired
    private IWpInfoDeptService wpInfoDeptService;

    /**
     * 查询三级承办单位处理情况汇总表列表
     */
    @PreAuthorize("@ss.hasPermi('getWpInfoDeptList')")
    @GetMapping("/getWpInfoDeptList")
    public AjaxResult getWpInfoDeptList(WpInfoDept wpInfoDept) throws ParseException {
        List<WpInfoDept> list = wpInfoDeptService.getWpInfoDeptList(wpInfoDept);

        if (StringUtils.isNotEmpty(list)){
            WpInfoDept wpInfoDeptCon = new WpInfoDept();

            wpInfoDeptCon.setDeptLevel2Name("总计");
            Integer wpInfoCount = list.stream().map(WpInfoDept::getWpInfoCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoDeptCon.setWpInfoCount(wpInfoCount);

            Integer fiveCompleteCount = list.stream().map(WpInfoDept::getFiveCompleteCount)
                    .reduce(Integer::sum)
                    .orElse(0);

            wpInfoDeptCon.setFiveCompleteCount(fiveCompleteCount);

            Integer tenCompleteCount = list.stream().map(WpInfoDept::getTenCompleteCount)
                    .reduce(Integer::sum)
                    .orElse(0);

            wpInfoDeptCon.setTenCompleteCount(tenCompleteCount);

            Integer ontimeCompleteCount = list.stream().map(WpInfoDept::getOntimeCompleteCount)
                    .reduce(Integer::sum)
                    .orElse(0);

            wpInfoDeptCon.setOntimeCompleteCount(ontimeCompleteCount);


            Integer timeoutCompleteCount = list.stream().map(WpInfoDept::getTimeoutCompleteCount)
                    .reduce(Integer::sum)
                    .orElse(0);

            wpInfoDeptCon.setTimeoutCompleteCount(timeoutCompleteCount);

            Integer timeoutUncompleteCount = list.stream().map(WpInfoDept::getTimeoutUncompleteCount)
                    .reduce(Integer::sum)
                    .orElse(0);

            wpInfoDeptCon.setTimeoutUncompleteCount(timeoutUncompleteCount);

            Integer completingCount = list.stream().map(WpInfoDept::getCompletingCount)
                    .reduce(Integer::sum)
                    .orElse(0);

            wpInfoDeptCon.setCompletingCount(completingCount);

            Integer recoverCount = list.stream().map(WpInfoDept::getRecoverCount)
                    .reduce(Integer::sum)
                    .orElse(0);

            wpInfoDeptCon.setRecoverCount(recoverCount);

            //超时未完结 + 超时已办结 = 延期工单数 Delay
            wpInfoDeptCon.setDelayCount(timeoutUncompleteCount+timeoutCompleteCount);
            list.add(wpInfoDeptCon);
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出三级承办单位处理情况汇总表列表
     */
    @Log(title = "三级承办单位处理情况汇总表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('wpInfoDept:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WpInfoDept wpInfoDept) throws ParseException {
        List<WpInfoDept> list = wpInfoDeptService.getWpInfoDeptList(wpInfoDept);
        for (int i = 0; i < list.size(); i++) {
            WpInfoDept infoDept = list.get(i);
            infoDept.setIndex(i+1);
        }
        ExcelUtil<WpInfoDept> util = new ExcelUtil<WpInfoDept>(WpInfoDept.class);
        util.exportExcel(response, list, "三级承办单位处理情况汇总表数据");
    }

    /**
     * 查询三级承办单位工单汇总工单列表
     *  三级承办单位处理汇总  根据数量查询出对应的工单列表
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('getWpInfoListByWpids')")
    @GetMapping("/getWpInfoListByWpids")
    public TableDataInfo getWpInfoListByWpids(WpInfo wpInfo) {

        String type = wpInfo.getType();
        //超时未完成，正在处理中
        if ("completingCount".equals(type)||"timeoutUncompleteCount".equals(type)){
            List<String> wpids = wpInfoDeptService.selectWpInfoListByWpids(wpInfo);

            /*PageDomain pageDomain = TableSupport.buildPageRequest();
            Integer pageNum = pageDomain.getPageNum();
            Integer pageSize = pageDomain.getPageSize();
            List<String> wpidsCon = new ArrayList<>();

            int size = Math.min(wpids.size(), pageNum * pageSize);
            if (wpids.size() < (pageNum - 1) * pageSize) {
                return getDataTable(new ArrayList<>(), wpids);
            }
            for (int i = (pageNum - 1) * pageSize; i < size; i++) {
                wpidsCon.add(wpids.get(i));
            }
            if (wpidsCon.size()==0){
                return getDataTable(new ArrayList<>(), wpids);
            }*/
            if (wpids.size()==0){
                TableDataInfo tableDataInfo = new TableDataInfo();
                tableDataInfo.setCode(HttpStatus.SUCCESS);
                tableDataInfo.setMsg("查询成功！");
                return tableDataInfo;
            }
            List<WpInfo> wpInfos = wpInfoDeptService.findWpInfoListByWpids(wpids);

            TableDataInfo tableDataInfo = new TableDataInfo();
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("查询成功！");
            tableDataInfo.setRows(wpInfos);
            return tableDataInfo;
        }
      //  startPage();
        TableDataInfo tableDataInfo = wpInfoDeptService.getWpInfoListByWpids(wpInfo);
        return tableDataInfo;
    }

    @PreAuthorize("@ss.hasPermi('getWpInfoListByWpids:export')")
    @PostMapping("/getWpInfoListByWpids/export")
    public void getWpInfoListByWpidsExport(HttpServletResponse response, WpInfo wpInfo) {
        String type = wpInfo.getType();
        //超时未完成，正在处理中
        if ("completingCount".equals(type)||"timeoutUncompleteCount".equals(type)){
            List<String> wpids = wpInfoDeptService.selectWpInfoListByWpids(wpInfo);

            if (wpids.size()==0){
                return ;
            }
            ExcelUtil<WpInfo> util = new ExcelUtil<WpInfo>(WpInfo.class);
            List<WpInfo> wpInfos = wpInfoDeptService.findWpInfoListByWpids(wpids);
            util.exportExcel(response, wpInfos, "统计工单列表");;
        }
        TableDataInfo tableDataInfo = wpInfoDeptService.getWpInfoListByWpids(wpInfo);
        List<WpInfo> rows = (List<WpInfo>) tableDataInfo.getRows();
        ExcelUtil<WpInfo> util = new ExcelUtil<WpInfo>(WpInfo.class);
        util.exportExcel(response, rows, "统计工单列表");
    }




}
